package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.elm.model.Credit;
import cn.edu.tju.elm.service.CreditService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/credits")
@Tag(name="管理积分", description = "提供对积分的增删改查功能")
public class CreditController {

    @Autowired
    private CreditService creditService;

    @GetMapping("/total")
    @Operation(summary = "获取用户积分总数", description = "返回指定用户的积分总数")
    public HttpResult<Integer> getTotalCreditByUserId(@RequestParam Long userId) {
        try {
            Integer total = creditService.getTotalCreditByUserId(userId);
            return HttpResult.success(total);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    @GetMapping("/available")
    @Operation(summary = "获取用户可用积分", description = "返回指定用户的可用积分列表")
    public HttpResult<List<Credit>> getAvailableCreditByUserId(@RequestParam Long userId) {
        try {
            List<Credit> credits = creditService.getAvailableCreditByUserId(userId);
            return HttpResult.success(credits);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户所有积分记录", description = "返回指定用户的积分明细")
    public HttpResult<List<Credit>> getCreditByUserId(@RequestParam Long userId) {
        try {
            List<Credit> credits = creditService.getCreditByUserId(userId);
            return HttpResult.success(credits);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    @GetMapping("/user/channel")
    @Operation(summary = "按渠道类型获取用户积分", description = "返回指定用户指定渠道类型的积分")
    public HttpResult<List<Credit>> getCreditByUserIdAndChannelType(
            @RequestParam Long userId,
            @RequestParam Integer channelType) {
        try {
            List<Credit> credits = creditService.getCreditByUserIdAndChannelType(userId, channelType);
            return HttpResult.success(credits);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }

    @PostMapping("")
    @PreAuthorize("hasAuthority('ADMIN')")
    @Operation(summary = "添加积分记录", description = "为用户添加新的积分记录，需要管理员权限")
    public HttpResult<Credit> insertCredit(@RequestBody Credit credit) {
        try {
            Credit saved = creditService.insertCredit(credit);
            return HttpResult.success(saved);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, e.getMessage());
        }
    }
}
